package com.domor.xxx.system.controller.system;

import com.domor.xxx.common.core.controller.BaseController;
import com.domor.xxx.common.core.domain.AjaxResult;
import com.domor.xxx.common.core.page.TableDataInfo;
import com.domor.xxx.framework.shiro.util.ShiroUtils;
import com.domor.xxx.system.domain.SysMsg;
import com.domor.xxx.system.service.SysMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h3>系统消息 控制层</h3>
 * @author liyuyang
 * @date 2020/5/15
 **/
@Controller
@RequestMapping("/system/msg")
public class SysMsgController extends BaseController {
    private String prefix = "system/msg";

    @Autowired
    private SysMsgService msgService;

    @GetMapping()
    public String msg() {
        return prefix + "/index";
    }

    /**
     * 查询系统消息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMsg msg) {
        setOrderByAndPage();
        msg.setReceiver(ShiroUtils.getLoginName());
        List<SysMsg> list = msgService.selectList(msg);
        return getDataTable(list);
    }

    /**
     * 查询个人消息列表(首页提醒)
     */
    @PostMapping("/getSelfMsg")
    @ResponseBody
    public Object getSelfMsg() {
        SysMsg sysMsg = new SysMsg();
        sysMsg.setReceiver(ShiroUtils.getLoginName());
        sysMsg.setStatus(0);
        List<SysMsg> unSend = msgService.selectList(sysMsg);
        Map<String, Object> result = new HashMap<>();
        result.put("unSend", unSend);
        result.put("unRead", msgService.countUnRead(ShiroUtils.getLoginName()));
        return result;
    }


    /**
     * 批量修改设置消息状态
     * 返回未读消息数量
     */
    @PostMapping("/read")
    @ResponseBody
    public AjaxResult read(String ids) {
        msgService.readMsgByIds(ids);
        int unRead = msgService.countUnRead(ShiroUtils.getLoginName());
        return success(unRead);
    }

    /**
     * 设置全部消息为已读
     */
    @PostMapping("/readAll")
    @ResponseBody
    public AjaxResult readAll() {
        return toAjax(msgService.readMsgByReceiver(ShiroUtils.getLoginName()));
    }

    /**
     * 根据接收人设置消息为发送
     */
    @PostMapping("/sentMsgByReceiver")
    @ResponseBody
    public AjaxResult sentMsgByReceiver() {
        return toAjax(msgService.sentMsgByReceiver(ShiroUtils.getLoginName()));
    }

    /**
     * 根据ID设置消息为发送
     */
    @PostMapping("/sentMsgById")
    @ResponseBody
    public AjaxResult sentMsgById(int msgId) {
        return toAjax(msgService.sentMsgById(msgId));
    }

}
